using System;
using System.IO;
using System.Runtime.InteropServices;
using Microsoft.VisualStudio.Package;
using Microsoft.VisualStudio;
using MH.WsdlWorks.ProjectModel;

namespace MH.WsdlWorks
{
    [CLSCompliant(false)]
    [ComVisible(true)]
    [Guid("71B0372D-4028-4c60-82A4-1B1E8D7AF311")]
    public class RequestNode : NodeBase
    {
        // type guid of an Operation Node
        readonly Guid typeGuid = new Guid("DB78510F-AC61-4818-A2C5-B903098A6990");
        readonly Request request;

        public Request Request
        {
            get { return request; }
        }

        public RequestNode(SimpleProjectNode root, Request request) 
            : base(root, request.GetPath())
        {
            this.request = request;
            AddFileNode(request.RequestFile);
            foreach (MessageFile responseFile in request.ResponseFiles)
            {
                AddFileNode(responseFile);
            }
        }

        protected override SimpleNodeProperties CreatePropertiesObject()
        {
            return new RequestNodeProperties(this);
        }

        public override object GetIconHandle(bool open)
        {
            // TODO: create and return a request node icon
            return this.ProjectMgr.ImageHandler.GetIconHandle((int)ProjectNode.ImageName.Reference);
        }

        public override Guid ItemTypeGuid
        {
            get { return this.typeGuid; }
        }

        private MessageFileNode AddFileNode(MessageFile file)
        {
            if (file == null)
                return null;

            string itemType = "Content"; // this is the compile directive
            SimpleProjectElement projectElement = ((SimpleProjectNode)this.ProjectMgr).CreateMsBuildFileItem(
                file.GetPath(), itemType);
            MessageFileNode fileNode = new MessageFileNode(this.ProjectMgr, projectElement, file);
            this.AddChild(fileNode);
            return fileNode;
        }

        public override int MenuCommandId
        {
            get
            {
                return MenuIdList.menuIdRequestNode;
            }
        }

        public override int SortPriority
        {
            get
            {
                return DefaultSortOrderNode.FolderNode;
            }
        }

        protected override int QueryStatusOnNode(Guid guidCmdGroup, uint cmd, IntPtr pCmdText, ref QueryStatusResult result)
        {
            if (guidCmdGroup == GuidList.guidTestMenuPackageCmdSet)
            {
                if (cmd == PackageCommandIdList.cmdidRun)
                {
                    result |= QueryStatusResult.SUPPORTED | QueryStatusResult.ENABLED;
                    return VSConstants.S_OK;
                }
            }
            return base.QueryStatusOnNode(guidCmdGroup, cmd, pCmdText, ref result);
        }

        protected override int ExecCommandOnNode(Guid guidCmdGroup, uint cmd, uint nCmdexecopt, IntPtr pvaIn, IntPtr pvaOut)
        {
            if (guidCmdGroup == GuidList.guidTestMenuPackageCmdSet)
            {
                if (cmd == PackageCommandIdList.cmdidRun)
                {
                    RunRequest();
                    return VSConstants.S_OK;
                }
            }
            return base.ExecCommandOnNode(guidCmdGroup, cmd, nCmdexecopt, pvaIn, pvaOut);
        }

        private void RunRequest()
        {
            MessageFile responseFile = request.RunRequest();
            MessageFileNode responseFileNode = AddFileNode(responseFile);
            responseFileNode.OpenFile();
        }
    }
}
